package cn.laixueit.controller;

import cn.laixueit.pojo.Classes;
import cn.laixueit.service.ClassesService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/classes")
public class ClassesController {

    @Autowired
    private ClassesService classesService;
    
    @PostMapping("/save")
    public String save(Classes classes){
        classesService.save(classes);
        return "redirect:page";
    }

    @GetMapping("/page")
    public String page(@RequestParam(defaultValue = "1")Integer start,
                       @RequestParam(defaultValue = "10")Integer size,
                       @ModelAttribute Classes classes,
                       Model model){
        PageInfo<Classes> pageInfo = classesService.page(start, size, classes);
        model.addAttribute("pageInfo",pageInfo);
        return "classList";
    }

    @GetMapping("/findClassesById")
    public String findClassesById(Integer id,Model model){
        Classes classes =  classesService.findById(id);
        model.addAttribute("classes",classes);
        return "classEdit";
    }

    @PostMapping("/update")
    public String update(Classes classes,Integer close){
        classesService.update(classes,close);
        return "redirect:page";
    }

    @GetMapping("/removeById")
    public String removeById(Integer id){
        classesService.removeById(id);
        return "redirect:page";
    }


}
